// pages/my/coupon/coupon.js
var app = getApp();
var couponType;
var listOffset;
var dealtype;
var conditionval;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    blankshow: false,
    hasMore: true,
    blanktips: '抱歉，你暂时没有优惠券~',
    couponlist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    dealtype = '';
    conditionval = -0.01;
    if (options.dealtype) {
      if (options.dealtype == 'selected') {
        dealtype = options.dealtype;
        conditionval = parseFloat(options.money);
        this.setData({
          dealtype:dealtype
        });
      }
    }
    couponType = '0';
    listOffset = 0;
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          listhgt: res.windowHeight,
          couponType: couponType
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.showLoading();
    this.loadCouponList(couponType, listOffset);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  loadCouponList: function (cType, offset) {
    var url = '/index.php/api/User/coupon';
    var params = {
      type: cType,
      page: offset
    };
    app.request(url, params).then(this.couponListCallBack.bind(this));
  },
  couponListCallBack: function (res) {
    // console.log(res);
    app.hideLoading();
    var dataArr = res.result.coupon_list;
    if (dataArr.length <= 0) {
      var blankshow = false;
      if (listOffset == 0) {
        blankshow = true;
      }
      this.setData({
        blankshow: blankshow
      });
      return;
    }
    else {
      if (dataArr.length < 10) {
        this.setData({
          hasMore: false
        });
      }
    }
    var couponlist = [];
    if (listOffset > 0) {
      couponlist = this.data.couponlist;
    }
    for (var i = 0; i < dataArr.length; i++) {
      var coupon = {};
      coupon.name = dataArr[i].name;
      coupon.couponid = dataArr[i].id;
      coupon.condition = dataArr[i].condition;
      if (conditionval > 0 && parseFloat(coupon.condition) > conditionval) {
        continue;
      }
      coupon.note = dataArr[i].use_scope + '，满' + coupon.condition + '使用';
      coupon.value = dataArr[i].money;
      coupon.validdate = app.formatDateTime(dataArr[i].use_start_time, false) + ' ~ ' + app.formatDateTime(dataArr[i].use_end_time, false);
      coupon.status = couponType;
      couponlist.push(coupon);
    }
    this.setData({
      couponlist: couponlist,
      blankshow: false
    });
  },
  loadMoreCoupon: function () {
    if (this.data.hasMore == false) {
      return;
    }
    listOffset++;
    app.showLoading();
    this.loadCouponList(couponType, listOffset);
  },
  changeCouponType: function (event) {
    couponType = event.currentTarget.dataset.coupontype;
    var tips = '';
    if (couponType == '0') {
      tips = '抱歉，你暂时没有未使用的优惠券';
    }
    else if (couponType == '1') {
      tips = '抱歉，你暂时没有已使用的优惠券';
    }
    else if (couponType == '2') {
      tips = '抱歉，你暂时没有已过期的优惠券';
    }
    this.setData({
      couponType: couponType,
      blanktips: tips
    });
    listOffset = 0;
    app.showLoading();
    this.loadCouponList(couponType, listOffset);
  },
  getCoupon: function () {
    wx.navigateTo({
      url: './getcoupon',
    });
  },
  dealCouponSelect: function (event) {
    if (dealtype != 'selected') {
      return;
    }
    dealtype = '';
    if (couponType != '0') {
      wx.showToast({
        icon: 'none',
        title: '无效的优惠券',
      });
      return;
    }
    var coupon = event.currentTarget.dataset.coupon;
    app.globalData.coupon.status = 1;
    app.globalData.coupon.value = coupon;
    wx.navigateBack({});
  }
})